//
//  WZSearchController.m
//  ETong
//
//  Created by wanzhao on 15/12/28.
//  Copyright © 2015年 wanzhao. All rights reserved.
//

#import "WZSearchController.h"
#import "WZQuestionCell.h"

@implementation WZSearchController

- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
    [self.searchContentsController.navigationController setNavigationBarHidden: NO animated: NO];
    [super setActive: visible animated: NO];
    self.searchBar.hidden = !visible;
}


- (id)initWithSearchBar:(UISearchBar *)searchBar contentsController:(UIViewController *)viewController
{
    self = [super initWithSearchBar:searchBar contentsController:viewController];
    if (self) {
        // Custom initialization
        self.searchSectionListArray = [NSMutableArray array];
        self.searchAllFirstLetter = [NSMutableArray array];
        self.searchMessageArray = [NSMutableArray array];
        _editingStyle = UITableViewCellEditingStyleNone;
        
        self.searchResultsDataSource = self;
        self.searchResultsDelegate = self;
        self.delegate = self;
        self.searchResultsTitle = NSLocalizedString(@"searchResults", @"The search results");
        
        self.searchResultsTableView.sectionIndexColor = RGBCOLOR(0x64, 0x64, 0x64);
        self.searchResultsTableView.sectionIndexBackgroundColor = ClearColor;
        self.searchResultsTableView.sectionIndexTrackingBackgroundColor = ClearColor;

    }
    return self;
}
-(void)setSearchMessageArray:(NSMutableArray *)searchMessageArray
{
    _searchMessageArray = searchMessageArray;
}
- (void)setSearchSectionListArray:(NSMutableArray *)searchSectionListArray
{
    _searchSectionListArray = searchSectionListArray;
}

- (void)setSearchAllFirstLetter:(NSMutableArray *)searchAllFirstLetter
{
    _searchAllFirstLetter = searchAllFirstLetter;
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    if (_searchMessageArray.count != 0) {
        return 1;
    }
    else
    {
    return self.searchSectionListArray.count;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (_searchMessageArray.count != 0) {
        return _searchMessageArray.count;
    }
    else
    {
    NSArray *rowArray = self.searchSectionListArray[section];
    return [rowArray count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_cellForRowAtIndexPathCompletion) {
        return _cellForRowAtIndexPathCompletion(tableView, indexPath);
    }
    else{
        return nil;
    }
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    if (_canEditRowAtIndexPath) {
        return _canEditRowAtIndexPath(tableView, indexPath);
    }
    else{
        return NO;
    }
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_heightForRowAtIndexPathCompletion) {
        return _heightForRowAtIndexPathCompletion(tableView, indexPath);
    }
    
    return 50;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.editingStyle;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_didSelectRowAtIndexPathCompletion) {
        return _didSelectRowAtIndexPathCompletion(tableView, indexPath);
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_didDeselectRowAtIndexPathCompletion) {
        _didDeselectRowAtIndexPathCompletion(tableView, indexPath);
    }
}





#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (_searchMessageArray.count != 0) {
        return 0.01;
    }
    return 35.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    CGFloat height = 0.01f;
    switch (section) {
        case 1:
            height = 0.01f;
            break;
        default:
            break;
    }
    return height;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section   // custom view for header. will be adjusted to default or specified header height
{
    
    if (self.searchMessageArray.count != 0) {
        return nil;
    }
    UIView *headerView = nil;
    headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, 35.0f);
    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    UIView *beginLine = [[UIView alloc] init];
    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
    [headerView addSubview:beginLine];
    
    UIView *endLine = [[UIView alloc] init];
    endLine.frame = CGRectMake(0, 35.0f-0.5, tableView.frame.size.width, 0.5);
    [endLine setBackgroundColor:KSEPERATORLINECOLOR];
    [headerView addSubview:endLine];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = _searchAllFirstLetter[section];
    [label setTextColor:RGBCOLOR(0x92, 0x92, 0x92)];
    label.frame = CGRectMake(15, 1, 200, 35.0f-1);
    [headerView addSubview:label];

    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section   // custom view for footer. will be adjusted to default or specified footer height
{
    UIView *headerView = nil;
    //    if(section != 1)
    //    {
    //        return headerView;
    //    }
    //    headerView = [[UIView alloc] init];
    //    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, 2);
    //    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    //    UIView *beginLine = [[UIView alloc] init];
    //    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
    //    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
    //    [headerView addSubview:beginLine];
    return headerView;
    
}

//
//- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
//{
//    return _searchAllFirstLetter;
//}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return index;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
          }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
    
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0)
{
    return @"删除";
}



//bug 8510 fixed by lixu
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView NS_DEPRECATED_IOS(3_0,8_0){
    [tableView setContentInset:UIEdgeInsetsZero];
    [tableView setScrollIndicatorInsets:UIEdgeInsetsZero];
    tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

{ for(UIView *subview in controller.searchResultsTableView.subviews) {
    
    if([subview isKindOfClass:[UILabel class]]) {
        
        [(UILabel*)subview setText:@"未找到匹配的信息"];
        
    }
    
}
    return YES;
}


#pragma mark-- UISearchDisplayDelegate

- (void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller NS_DEPRECATED_IOS(3_0,8_0)
{
    UIView *topView = controller.searchBar.subviews[0];
    
    for (UIView *subView in topView.subviews) {
        
        if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
            
            UIButton * cancelButton = (UIButton*)subView;
            
            [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
            
        }
        
    }
}


@end
